package server.container;

import server.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Context implements Valve, Lifecycle {
    private String docBase;
    private String path;

    private List<Wrapper> wrapperList;

    public String getDocBase() {
        return docBase;
    }

    public void setDocBase(String docBase) {
        this.docBase = docBase;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public List<Wrapper> getWrapperList() {
        return wrapperList;
    }

    public void setWrapperList(List<Wrapper> wrapperList) {
        this.wrapperList = wrapperList;
    }

    @Override
    public void invoke(Request request, Response response) {
        Wrapper wrapper = request.getWrapper();
        if (Objects.nonNull(wrapper)){
            wrapper.invoke(request,response);
        }else {
            try {
                response.outputHtml(request.getUrl());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void start() {
        wrapperList.forEach(Wrapper::start);
    }
}
